#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>


sem_t mysem1;
sem_t mysem2;
sem_t mysem3;
int a=100;


void *myfunc1(void *arg)
{

	while(1)
	{
		sem_wait(&mysem1);
		printf("1:%d\n",a);
		sleep(1);
		a++;
		sem_post(&mysem2);
	}
}
void *myfunc2(void *arg)
{
	while(1)
	{
		sem_wait(&mysem2);
		printf("2:%d\n",a);
		sleep(1);
		a++;
		sem_post(&mysem3);

	}
}

int main(int argc, const char *argv[])
{

	sem_init(&mysem1,0,1);
	sem_init(&mysem2,0,0);
	sem_init(&mysem3,0,0);
	pthread_t mypthread1;
	pthread_create(&mypthread1,NULL,&myfunc1,NULL);

	pthread_t mypthread2;
	pthread_create(&mypthread2,NULL,&myfunc2,NULL);

	while(1)
	{
		sem_wait(&mysem3);
		printf("zhu:a=%d\n",a);		
		sleep(1);
		a++;
		sem_post(&mysem1);
	}

	return 0;
}
